package com.itheima.Jiuye15;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ThreadSoket implements Runnable {
    private Socket accept;
    public ThreadSoket(Socket accept) {
        this.accept = accept;
    }

    @Override
    public void run() {
        BufferedOutputStream bfo = null;
        try {
            //网络中的流，用于读取客户端数据
            BufferedInputStream bfis = new BufferedInputStream(accept.getInputStream());
            //创建本地io流，把数据写到本地实现永久存储
             bfo = new BufferedOutputStream(new FileOutputStream("C:\\Users\\纹龍\\Desktop\\"+ UUID.randomUUID().toString() +".jpg"));
            int a ;
            while ((a = bfis.read()) != -1){
                bfo.write(a);
            }
            //bfos.flush();
            //accept.shutdownOutput();

            BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bf.write("传输成功");
            bf.newLine();
            bf.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bfo != null) {
                try {
                    bfo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept != null) {
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
